<?php
namespace app\backend\controller;

use app\backend\controller\traits\AuthManager;
use app\backend\model\Bank;
use app\backend\model\LoginLog;
use app\backend\model\Member;
use app\backend\model\Photo;
use app\backend\model\Withdraw;
use think\Controller;
use think\Image;
use Business\sms\VComSms;
use Business\Business;

class MemberController extends Controller
{
    use AuthManager;
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 判断用户是否登录
     * @date   2017-1-20
     * @author wuhao
     *
     * @return json
     */
    public function checkLogin()
    {
        $memberId = $this->isLogin();
        if (!$memberId) {
            $this->apiReturn(400, [], '请先登录');
        } else {
            $this->apiReturn(200, [], '');
        }
    }

    /**
     * 会员注册
     * @date   2017-02-07
     * @author wuhao
     *
     * @param  string  $name      昵称
     * @param  string  $mobile    手机号
     * @param  string  $vcode     短信验证码
     * @param  string  $password  密码
     * @param  string  $dtype     账号类型
     * @param  string  $idCard    身份证号或营业执照
     * @param  int     $userType  用户类型用户为租户时 1=企业 0=个人
     * @return
     */
    public function registerAction()
    {
        $nickname = I('post.name', '', 'strval');
        $mobile   = I('post.mobile', '', 'strval');
        $vcode    = I('post.vcode', '', 'strval');
        $password = I('post.password', '', 'strval');
        $dtype    = I('post.dtype', '', 'intval');
        $idCard   = I('post.id_card', '', 'strval');
        $userType = I('post.user_type', 0, 'intval');

        if (!$nickname || !$mobile || !$vcode || !$password || !in_array($dtype, [0,1,2,3,4,5,6]) ||!$idCard || !in_array($userType, [0,1])) {
            $this->apiReturn(406, [], '参数错误');
        }

        if (!ismobile($mobile)) {
            $this->apiReturn(405, [], '手机号格式错误');
        }
        $memberModel = new Member();
        if ($memberModel->getMemberInfo(['mobile' => $mobile], 'id')) {
            $this->apiReturn(405, [], '手机号已存在');
        }

        if ($userType == 0) {//类型为企业时输入的是营业执照，不判断身份证格式
            if (!isIDCard($idCard)) {
                $this->apiReturn(407, [], '身份证格式错误');
            }
            if ($memberModel->getMemberInfo(['id_card' => $idCard], 'id')) {
                $this->apiReturn(407, [], '身份证已存在');
            }
        } else {
            if ($memberModel->getMemberInfo(['id_card' => $idCard], 'id')) {
                $this->apiReturn(407, [], '营业执照已存在');
            }
        }

        //检查密码
        $res = chkPassword1($password);
        if ($res !== true) {
            $this->apiReturn(404, [], '密码格式错误' . $res);
        }

        //检查验证码
        $sms = new VComSms();
        $check = $sms->ChkVerifyCode($mobile, 'reg', $vcode);
        if (!$check) {
            $this->apiReturn(403, [], '手机验证码错误');
        } elseif ($check === -1) {
            $this->apiReturn(403, [], '验证码已经失效');
        }

        //生成登录账号
        $business = new Business();
        $account  = $business->_generateAccount($dtype);

        $data   = [
            'dtype'       => $dtype,
            'username'    => $account,
            'mobile'      => $mobile,
            'password'    => md5(md5($password)),
            'withdraw_pwd'    => '',
            'status'      => 1,
            'nickname'    => $nickname,
            'id_card'     => $userType == 0 ? $idCard : '',
            'license'     => $userType == 1 ? $idCard : '',
            'user_type'   => $userType,
            'update_time' => time(),
            'create_time' => time(),
        ];
        $res = $memberModel->addMember($data);
        if ($res) {
            $tmp = [
                'mid'   => $res,
                'money' => 0,
            ];
            $resAddMoney = $memberModel->addMemberMoney($tmp);
            if (!$resAddMoney) {
                zft_log('add_money_fail', json_encode($tmp));
            }
            $this->apiReturn(200, ['username' => $account], '注册成功');
        } else {
            zft_log('register', $memberModel->getLastSql());
            $this->apiReturn(400, [], '注册失败');
        }
    }

    /**
     * 获取会员信息
     * @date   2017-1-3
     * @author Lanlc
     *
     * @param  string  $paramType   搜索参数类型
     * @param  string  $paramValue  搜索参数值
     * @param  int     $userType    用户类型
     *
     * @return json
     */
    public function index()
    {
        $memberInfo = $this->getLoginInfo();
        $memberId = $memberInfo['mid'];
        $parentId = $memberInfo['p_mid'];
        $auth = $this->getPower($memberId, $memberInfo['dtype']);
        if (!in_array($this->getOper('member_index'), $auth)) {
            $this->apiReturn(400,[],'无权操作');
        }

        $paramType  = I('post.paramType', '', 'strval');
        $paramValue = I('post.paramValue', '', 'strval');
        $userType   = I('post.userType', '', 'intval');
        $page       = I('post.page', 1, 'intval');
        $pageSize   = I('post.page_size', 10, 'intval');

        $where['m.status'] = 1;

        switch ($paramType) {
            case 'mobile': $where['m.mobile']   = $paramValue; break;
            case 'username': $where['m.username'] = ['like', '%'.$paramValue.'%']; break;
            case 'nickname': $where['m.nickname'] = ['like', '%'.$paramValue.'%']; break;
            default: break;
        }

        if ($userType !=='') {
            $where['m.dtype'] = $userType;
        }
        $memberModel = new Member();

        if (in_array($memberInfo['p_dtype'], [3, 4])) {
            $where['r.parent_id'] = $parentId;
            $where['son_id_type'] = 0; //普通关系非员工
            $list =  $memberModel->getOwnerMembers($page, $pageSize, $where);
        } else {
            $list =  $memberModel->getMembers($page, $pageSize, $where);
        }
        if ($list) {
            foreach ($list['list'] as $k => $v) {
                $list['list'][$k]['money'] = round($v['money']/100, 2);
            }
            $this->apiReturn(200, $list, '');
        } else {
            $this->apiReturn(201, [], '没有相关数据');
        }
    }

    /**
     * 新建用户
     * @date   2017-1-03
     * @author Lanlc
     *
     * @param  int     $dtype     用户类型
     * @param  string  $nickname  用户昵称
     * @param  string  $mobile    用户手机号
     * @param  string  $idCard    用户身份证或营业执照
     * @param  string  $address   用户地址
     * @param  string  $password  用户密码
     * @param  string  $header    用户头像
     * @param  int     $userType  用户类型为租户时 1=企业 0=个人
     *
     * @return
     */
    public function add()
    {
        $memberInfo = $this->getLoginInfo();
        $pmid     = $memberInfo['p_mid'];
        $auth = $this->getPower($memberInfo['mid'], $memberInfo['dtype']);
        if (!in_array($this->getOper('member_add'), $auth)) {
            $this->apiReturn(400,[],'无权操作');
        }
        $dtype    = I('post.dtype', '', 'intval');
        $nickname = I('post.nickname', '', 'strval');
        $mobile   = I('post.mobile', '', 'strval');
        $idCard   = I('post.id_card', '', 'strval');
        $address  = I('post.address', '', 'strval');
        //$bank     = I('post.bank', '', 'strval');
        //$bankCard = I('post.bankCard', '', 'strval');
        $password = I('post.password', '', 'strval');
        $header   = I('post.header', '', 'strval');
        $userType = I('post.user_type', 0, 'intval');

        $memberModel = new Member();
        if ($memberInfo['p_dtype'] === 0) {
            //管理员可添加所有类型的
            if (!in_array($dtype, [0,1,2,3,4,5,6])) {
                $this->apiReturn(400, [], '用户类型有误');
            }
        } else {
            //运营方,托管添加业主，租户，物业，水电
            if (!in_array($dtype, [1,2,5,6])) {
                $this->apiReturn(400, [], '用户类型有误');
            }
        }

        if (!$password ||!$mobile ||!$idCard  ||!$nickname || !in_array($userType, [0,1])) {
            $this->apiReturn(400, [], '参数错误');
        }

        if ($memberModel->getMemberInfo(['mobile' => $mobile], 'id')) {
            $this->apiReturn(205, [], '手机号已存在');
        }


        if ($userType == 0) {//类型为企业时输入的是营业执照，不判断身份证格式
            if (!isIDCard($idCard)) {
                $this->apiReturn(407, [], '身份证格式错误');
            }
            if ($memberModel->getMemberInfo(['id_card' => $idCard], 'id')) {
                $this->apiReturn(407, [], '身份证已存在');
            }
        } else {
            if ($memberModel->getMemberInfo(['id_card' => $idCard], 'id')) {
                $this->apiReturn(407, [], '营业执照已存在');
            }
        }

        //生成登录账号
        $business = new Business();
        $username = $business->_generateAccount($dtype);

        $data = [
            'dtype'        => $dtype,
            'nickname'     => $nickname,
            'mobile'       => $mobile,
            'address'      => $address,
            'username'     => $username,
            'id_card'      => $userType == 0 ? $idCard : '',
            'license'      => $userType == 1 ? $idCard : '',
            'user_type'    => $userType,
            'password'     => md5(md5($password)),
            'withdraw_pwd' => '',
            'create_time'  => time(),
            'update_time'  => time(),
        ];

        if ($mid = $memberModel->addMember($data)) {
            //添加关联信息
            $value = ['parent_id' => $pmid, 'son_id' => $mid, 'son_id_type' => 0, 'ctime' => time()];
            $res = $memberModel->addRelation($value);
            if (!$res) {
                $this->apiReturn(202, [], '用户关系关联失败，请联系客服人员!');
            }

            //添加头像
            $photoModel = new Photo();
            if ($header) {
                $photoModel->addPhoto($mid, 4, 1, $header, $pmid);
            }

            //新建用户添加余额信息
            $tmp = [
                'mid'   => $mid,
                'money' => 0,
            ];
            $resAddMoney = $memberModel->addMemberMoney($tmp);
            if (!$resAddMoney) {
                zft_log('add_money_fail', json_encode($tmp));
            }

            $this->apiReturn(200, ['username' => $username], '创建成功');
        } else {
            zft_log('member/add', $memberModel->getLastSql());
            $this->apiReturn(400, [], '创建失败');
        }
    }

    /**
     * 检测手机号码
     * @author lanlc
     * @date   2017-01-09
     *
     * @param $mobile 手机号码
     *
     * @return
     */
    public function checkMobile()
    {
        $mobile = I('post.mobile');
        if (!$mobile) {
            $this->apiReturn(406, [], '参数错误');
        }
        //手机号码验证
        if (!ismobile($mobile)) {
            $this->apiReturn(406, [], '手机号码不正确');
        }
        $memberModel = new Member();
        if ($memberModel->getMemberInfo(['mobile' => $mobile], 'id')) {
            $this->apiReturn(203, [], '手机号已存在');
        } else {
            $this->apiReturn(200, ['is_register' => 0], '没有注册');
        }
    }

    /**
     * 检测用户名
     * @author lanlc
     * @date   2017-01-09
     *
     * @param $account 平台账号
     *
     * @return
     */
    public function checkAccount()
    {
        $account = I('post.account');

        if (!$account) {
            $this->apiReturn(406, [], '参数错误');
        }

        $memberModel = new Member();
        if ($memberModel->getMemberInfo(['username' => $account], 'id')) {
            $this->apiReturn(203, [], '用户名已存在');
        } else {
            $this->apiReturn(200, ['is_register' => 0], '没有注册');
        }
    }

    /**
     * [delete 删除用户数据(伪删除)]
     * @param  [type] $id [表ID]
     * @return [type]     [description]
     */
    public function delete()
    {
        $memberId = $this->isLogin();
        $mid = I('post.mid', '', 'intval');
        $user = new Member();
        $data['status'] = -1;
        $res = $user->updateUser($mid, $data);
        if ($res) {
            $this->apiReturn(200, [], '删除成功');
        } else {
            $this->apiReturn(201, [], '删除失败');
        }

        // 真.删除，不想用伪删除，请用这段
        // $user = Member::get($id);
        // if ($user) {
        //     $user->delete();
        //     $data['error'] = 0;
        // 	$data['msg'] = '删除成功';
        // } else {
        // 	$data['error'] = 1;
        // 	$data['msg'] = '删除失败';
        // }
        // return $data;
    }

    public function delete_image($id)
    {
        $memberId = $this->isLogin();
        $user = Member::get($id);
        if (file_exists($this->data['upload_path'] .'/'. $user->avatar)) {
            @unlink($this->data['upload_path'] .'/'. $user->avatar);
        }

        $source_image = str_replace('_thumb', '', $user->avatar);
        if (file_exists($this->data['upload_path'] .'/'. $source_image)) {
            @unlink($this->data['upload_path'] .'/'. $source_image);
        }

        $data['id'] = $id;
        $data['avatar'] = '';
        if ($user->update($data)) {
            return $this->success('图像删除成功', $this->data['module_url'].$id);
        } else {
            return $user->getError();
        }
    }

    public function update_expire_time($id)
    {
        $memberId = $this->isLogin();
        $user = Member::get($id);
        $data['id'] = $id;
        $user->expire_time = time(); //
        if (false !== $user->save()) {
            $data['expire_time'] = $user->expire_time;
            $data['error'] = 0;
            $data['msg'] = '更新成功';
        } else {
            $data['error'] = 1;
            $data['msg'] = '更新失败';
        }
        return $data;
    }

    /**
     * 获取物业公司信息
     * @date   2017-1-9
     * @author Lanlc
     *
     *
     * @return
     */
    public function getProperty()
    {
        $memberId = $this->isLogin();
        $memberModel = new Member();
        $res = $memberModel->getOperatorMmeber(5);
        if ($res) {
            $this->apiReturn(200, ['list' => $res], '');
        } else {
            $this->apiReturn(201, [], '没有相关数据');
        }
    }

    /**
     * 获取水电公司信息
     * @date   2017-1-9
     * @author Lanlc
     *
     *
     * @return
     */
    public function getHydropower()
    {
        $memberId = $this->isLogin();
        $memberModel = new Member();
        $res = $memberModel->getOperatorMmeber(6);
        if ($res) {
            $this->apiReturn(200, ['list' => $res], '');
        } else {
            $this->apiReturn(201, [], '没有相关数据');
        }
    }

    /**
     * 增加房源时获取业主信息
     * @date   2017-01-11
     * @author Lanlc
     *
     *
     * @return
     */
    public function getOwners()
    {
        $memberId = $this->isLogin();
        $memberModel = new Member();
        $res = $memberModel->getOperatorMmeber(1);
        $res[] = ['nickname' => '自身', 'id' => $memberId];
        $this->apiReturn(200, ['list' => $res], '');
    }

    /**
     * 用户中心 - 获取个人基本信息
     * @date   2017-04-28
     * @author wuhao
     *
     * @return array
     */
    public function getSelfInfo()
    {
        $memberId = $this->isLogin();
        $memberModel = new Member();
        $memberInfo = $memberModel->getMemberInfoById($memberId, 'id, mobile, id_card, license, create_time, address, nickname, dtype, user_type');
        if ($memberInfo) {
            $memberInfo['create_time'] = date('Y年m月d日', $memberInfo['create_time']);
            $this->apiReturn(200, $memberInfo, '');
        } else {
            $this->apiReturn(201, [], '查不到相关信息');
        }
    }

    /**
     * 用户中心 - 获取个人账户信息
     * @date   2017-05-08
     * @author wuhao
     *
     * @return array
     */
    public function getAccountInfo()
    {
        $memberId = $this->isLogin();
        $memberModel = new Member();
        //获取支付宝账号和提现密码
        $memberInfo = $memberModel->getMemberInfoById($memberId, 'id, mobile, alipay, withdraw_pwd');
        $data['mobile']       = $memberInfo['mobile'];
        $data['withdraw_pwd'] = $memberInfo['withdraw_pwd'] ? 1 : 0;
        $data['alipay']       = $memberInfo['alipay'];
        //获取绑定的银行卡信息
        $bankModel = new Bank();
        $bankCardInfo = $bankModel->getBankInfoByMid($memberId);
        if ($bankCardInfo) {
            $bankCode = array_unique(array_column($bankCardInfo, 'bank_code'));
            //获取银行名字
            $codeInfo = $bankModel->getBankCodeInfoByCode($bankCode, 'code, bank');
            foreach ($bankCardInfo as $k => $v) {
                foreach ($codeInfo as $c) {
                    if ($c['code'] == $v['bank_code']) {
                        $bankCardInfo[$k]['bank_name'] = $c['bank'];
                        break;
                    }
                }
            }
            $data['count_bank'] = count($bankCardInfo);
            $data['bank'] = $bankCardInfo;
        } else {
            $data['count_bank'] = 0;
            $data['bank'] = [];
        }
        $this->apiReturn(200, $data, '');
    }

    /**
     * 用户中心 - 个人中心信息
     * @date   2017-04-28
     * @author wuhao
     *
     * @return array
     */
    public function getUserCenterInfo()
    {
        $memberId = $this->isLogin();
        $memberModel = new Member();
        $loginLogModel = new LoginLog();
        //基本信息
        $memberInfo = $memberModel->getMemberInfo(['id' => $memberId], 'username, dtype, mobile');
        if (!$memberInfo) {
            $this->apiReturn(201, [], '查不到相关信息');
        }
        //登录日志
        $logInfo = $loginLogModel->getLastLoginLog($memberId);
        //余额
        $memberMoney = $memberModel->getMemberMoney($memberId);
        $money = $memberMoney ? yuan($memberMoney) : 0;
        $data = [
            'user_info' => $memberInfo,
            'login_log' => $logInfo,
            'money'     => $money,
        ];
        $this->apiReturn(200, $data, '');
    }

    /**
     * 用户中心 - 获取个人余额信息
     * @date   2017-05-08
     * @author wuhao
     *
     * @return array
     */
    public function getMemberMoney()
    {
        $memberId = $this->isLogin();
        $memberModel = new Member();
        //余额，可提现金额， 冻结金额，提现中的金额
        $money = $withdrawMoney = $freezeMoney = $applyWithdrawMoney = 0 ;

        //余额
        $moneyFen = $memberModel->getMemberMoney($memberId);
        $money = $moneyFen ? yuan($moneyFen) : 0;

        //冻结金额
        $freezeMoneyFen = $memberModel->getFreezeMoney($memberId);
        $freezeMoney = yuan($freezeMoneyFen);
        //可提现金额
        $withdrawMoney = ($money > $freezeMoney) ? ($money - $freezeMoney) : 0;

        //提现中的金额
        $withdrawModel = new Withdraw();
        $applyWithdrawMoneyFen = $withdrawModel->getUntreatedWithdraw($memberId);
        $applyWithdrawMoney = $applyWithdrawMoneyFen ? yuan($applyWithdrawMoneyFen) : 0;
        $data = [
            'money'          => $money,
            'wd_money'       => $withdrawMoney,
            'freeze_money'   => $freezeMoney,
            'apply_wd_money' => $applyWithdrawMoney,
        ];
        $this->apiReturn(200, $data, '');
    }

    /**
     * 更新用户密码
     * @date   2017-05-02
     * @author wuhao
     *
     * @param  string  $old_pwd        旧密码
     * @param  string  $new_pwd        新密码
     * @param  string  $confirm_pwd    再次输入
     *
     * @return
     */
    public function updatePassword()
    {
        $memberId = $this->isLogin();
        $oldPassWd   = I('post.old_pwd', '', 'strval');
        $newPassWd   = I('post.new_pwd', '', 'strval');
        $passWdAgain = I('post.confirm_pwd', '', 'strval');
        if (!$oldPassWd|| !$newPassWd|| !$passWdAgain) {
            $this->apiReturn(400, [], '缺少参数');
        }

        $oldPassWd = md5(md5($oldPassWd));

        //检测原密码是否正确
        $memberModel = new Member();
        if (!$memberModel->checkOldPassword($memberId, $oldPassWd)) {
            $this->apiReturn(201, [], '原密码输入有误');
        }
        $res = chkPassword($newPassWd, $passWdAgain);
        if ($res !== true) {
            $this->apiReturn(202, [], $res);
        }
        //更新新密码
        if ($memberModel->resetPassword($memberId, $newPassWd)) {
            $this->apiReturn(200, [], '更新密码成功');
        } else {
            zft_log('member/update', $memberModel->getLastSql());
            $this->apiReturn(203, [], '更新失败，请重新操作');
        }
    }

    /**
     * 忘记密码重置
     * @date   2017-05-03
     * @author wuhao
     *
     * @param  int     $mobile         手机号
     * @param  string  $new_pwd        新密码
     * @param  string  $confirm_pwd    再次输入
     *
     * @return
     */
    public function resetPassword()
    {
        $mobile = I('post.mobile', '', 'strval');
        $newPwd = I('post.new_pwd', '', 'strval');
        $passWdAgain = I('post.confirm_pwd', '', 'strval');
        if (!$mobile || !$newPwd || !$passWdAgain) {
            $this->apiReturn(400, [], '缺少参数');
        }
        if (!ismobile($mobile)) {
            $this->apiReturn(405, [], '手机号格式错误');
        }
        $memberModel = new Member();
        if (!$memberModel->getMemberInfo(['mobile' => $mobile], 'id')) {
            $this->apiReturn(404, [], '手机号未注册');
        }
        $res = chkPassword($newPwd, $passWdAgain);
        if ($res !== true) {
            $this->apiReturn(202, [], $res);
        }
        //更新新密码
        $memberModel = new Member();
        if ($memberModel->resetPasswordByMobile($mobile, $newPwd)) {
            $this->apiReturn(200, [], '更新密码成功');
        } else {
            zft_log('member/update', $memberModel->getLastSql());
            $this->apiReturn(203, [], '更新失败，请重新操作');
        }
    }

    /**
     * 更新用户地址
     * @date   2017-05-04
     * @author wuhao
     *
     * @param  string  $address     地址
     *
     * @return
     */
    public function updateAddress()
    {
        $memberId = $this->isLogin();
        $address = I('post.address', '', 'strval');
        if (!$address) {
            $this->apiReturn(400, [], '缺少参数');
        }
        $memberModel = new Member();
        $res = $memberModel->updateUser($memberId, ['address' => $address]);
        if ($res) {
            $this->apiReturn(200, [], '更新成功');
        } else {
            $this->apiReturn(201, [], '更新出错，请稍后重试');
        }
    }

    /**
     * 更换手机号
     * @date   2017-05-04
     * @author wuhao
     *
     * @param  int     $old_mobile      原手机号
     * @param  string  $new_mobile      新手机号
     *
     * @return
     */
    public function changeMobile()
    {
        $memberId = $this->isLogin();
        $oldMobile = I('post.old_mobile', '', 'strval');
        $newMobile = I('post.new_mobile', '', 'strval');
        if (!$oldMobile || !$newMobile) {
            $this->apiReturn(405, [], '参数格式错误');
        }
        //手机号码验证
        if (!ismobile($oldMobile) || !ismobile($newMobile)) {
            $this->apiReturn(406, [], '手机号码不正确');
        }

        $memberModel = new Member();
        $memberInfo  = $memberModel->getMemberInfo(['id' => $memberId], 'id, mobile');
        if (!$memberInfo) {
            $this->apiReturn(404, [], '用户信息有误');
        }
        if ($oldMobile != $memberInfo[0]['mobile']) {
            $this->apiReturn(404, [], '非登录人');
        }
        if ($memberModel->getMemberInfo(['mobile' => $newMobile], 'id')) {
            $this->apiReturn(404, [], '手机号已经注册过了');
        }

        //更新手机号
        $res = $memberModel->updateUser($memberId, ['mobile' => $newMobile]);
        if ($res) {
            $this->changeMobileSession($newMobile);
            $this->apiReturn(200, [], '更新成功');
        } else {
            zft_log('member/update', $memberModel->getLastSql());
            $this->apiReturn(200, [], '更新出错');
        }
    }

    /**
     * 新增或修改支付宝账号
     * @date   2017-05-05
     * @author wuhao
     *
     * @param  string  $alipay    支付宝账号
     *
     * @return
     */
    public function setAlipay()
    {
        $memberId = $this->isLogin();
        $alipay = I('post.alipay', '', 'strval');
        if (!$alipay) {
            $this->apiReturn(406, [], '支付宝账号不能为空');
        }
        $memberModel = new Member();
        //先获取原有支付宝账号
        $memberInfo = $memberModel->getMemberInfoById($memberId, 'alipay');
        if ($memberInfo['alipay'] && $memberInfo['alipay'] == $alipay) {
            $this->apiReturn(406, [], '信息未作修改');
        }
        if ($memberInfo['alipay']) {//如果存在原有的支付宝账号
            //判断是否有提现申请到这个账户
            $wdModel = new Withdraw();
            $wdList = $wdModel->getListByAccount($memberInfo['alipay'], 0, 'id');
            if ($wdList) {
                $this->apiReturn(405, [], '存在未完成的提现申请，不能修改');
            }
        }
        //判断账号是否已绑定
        if ($memberModel->getMemberInfo(['alipay' => $alipay], 'id')) {
            $this->apiReturn(407, [], '支付宝账号已被绑定');
        }

        //更新支付宝账号
        $res = $memberModel->updateUser($memberId, ['alipay' => $alipay]);
        if ($res) {
            $this->apiReturn(200, [], '支付宝帐号更新成功');
        } else {
            zft_log('member/update', $memberModel->getLastSql());
            $this->apiReturn(200, [], '支付宝帐号更新出错');
        }
    }

    /**
     * 新增或修改提现密码
     * @date   2017-05-05
     * @author wuhao
     *
     * @param  string  $new_pwd      新提现密码
     * @param  string  $confirm_pwd  确认提现密码
     *
     * @return
     */
    public function setWithdrawPwd()
    {
        $memberId   = $this->isLogin();
        $newPwd     = I('post.new_pwd', '', 'strval');
        $confirmPwd = I('post.confirm_pwd', '', 'strval');
        if (!$newPwd|| !$confirmPwd) {
            $this->apiReturn(400, [], '缺少参数');
        }
        //检测密码合法性
        $check = chkWithdrawPwd($newPwd, $confirmPwd);
        if ($check !== true) {
            $this->apiReturn(400, [], $check);
        }
        $memberModel = new Member();
        //先获取原有的支付密码
        $memberInfo = $memberModel->getMemberInfoById($memberId, 'withdraw_pwd');
        if ($memberInfo['withdraw_pwd'] && $memberInfo['withdraw_pwd'] == md5(md5($newPwd))) {
            $this->apiReturn(406, [], '新密码和原密码一样');
        }
        //更新支付密码
        $res = $memberModel->updateUser($memberId, ['withdraw_pwd' => md5(md5($newPwd))]);
        if ($res) {
            $this->apiReturn(200, [], '提现密码更新成功');
        } else {
            zft_log('member/update', $memberModel->getLastSql());
            $this->apiReturn(200, [], '提现密码更新出错');
        }
    }
}
